package cn.tedu.charging.order.dao.repository.impl;

import cn.tedu.charging.order.dao.mapper.BillExceptionMapper;
import cn.tedu.charging.order.dao.mapper.BillFailMapper;
import cn.tedu.charging.order.dao.mapper.BillSuccessMapper;
import cn.tedu.charging.order.dao.repository.BillRepository;
import cn.tedu.charging.order.pojo.po.ChargingBillExceptionPO;
import cn.tedu.charging.order.pojo.po.ChargingBillFailPO;
import cn.tedu.charging.order.pojo.po.ChargingBillSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class BillRepositoryImpl implements BillRepository {
    @Autowired
    private BillSuccessMapper billSuccessMapper;
    @Autowired
    private BillFailMapper billFailMapper;
    @Autowired
    private BillExceptionMapper billExceptionMapper;
    @Override
    public void saveFailBill(ChargingBillFailPO fail) {
        billFailMapper.insert(fail);
    }

    @Override
    public void saveSuccessBill(ChargingBillSuccessPO success) {
        billSuccessMapper.insert(success);
    }

    @Override
    public long countSuccess(String orderNo) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("bill_id",orderNo);
        return billSuccessMapper.selectCount(queryWrapper);
    }

    @Override
    public long countFail(String orderNo) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("bill_id",orderNo);
        return billFailMapper.selectCount(queryWrapper);
    }

    @Override
    public void updateSuccessStatus(Integer id, Integer status) {
        //update charging_bill_success set bill_status=3 where id=?
        ChargingBillSuccessPO param=new ChargingBillSuccessPO();
        param.setBillStatus( status);
        param.setId(id);
        //直接调用更新
        billSuccessMapper.updateById(param);

    }

    @Override
    public ChargingBillSuccessPO selectSuccessByBillId(String billId) {
        //select * from charging_bill_success where bill_id=?
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("bill_id",billId);
        return billSuccessMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveExceptionBill(ChargingBillSuccessPO successPO) {
        //1.组织一个chargingBillExceptionPO对象
        ChargingBillExceptionPO exceptionPO=new ChargingBillExceptionPO();
        exceptionPO.setBillId(successPO.getBillId());
        exceptionPO.setBillStarttime(successPO.getChargingStartTime());
        exceptionPO.setCreateTime(new Date());
        exceptionPO.setDeleted(0);
        //2.调用insert 写入数据库
        billExceptionMapper.insert(exceptionPO);
    }

    @Override
    public void updateSuccess(ChargingBillSuccessPO success) {
        //1.where 条件 billId
        //2.其他非空属性 都是set
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("bill_id",success.getBillId());
        success.setBillId(null);
        billSuccessMapper.update(success,queryWrapper);
    }
}
